package java51.homew0614;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class ChoiceThread extends Thread {
	Socket s;

	public ChoiceThread(Socket s) {
		super();
		this.s = s;
	}

	@Override
	public void run() {
		System.out.println(
				"远程：" + s.getInetAddress() + "," + s.getPort() + ",本地：" + s.getLocalAddress() + "," + s.getLocalPort());
		System.out.println(s + "连接上服务器！！");
		try {
			InputStream in = s.getInputStream();
			OutputStream out = s.getOutputStream();
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			PrintWriter pw = new PrintWriter(out);
			while (true) {
				String s1 = br.readLine();
				System.out.println("客户端说选择：" + s1);
				if (s1.equals("4"))
					break;
				switch (s1) {
				case "1":
					pw.println("天气：晴天");
					pw.flush();
					break;
				case "2":
					pw.println("pm2.5：45");
					pw.flush();
					break;
				case "3":
					pw.println("空气湿度：40%");
					pw.flush();
					break;
				default:
					pw.println("无该选项");
					pw.flush();
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				s.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
